export const comRequest = ({
    url,
    method = 'GET',
    params = {}
}) => {
    // fetch(r,{
    //     method
    // })

    return new Promise((resolve, reject) => {
        //发送一个get请求是这样的:

        //首先实例化一个XMLHttpRequest对象
        var httpRequest = new XMLHttpRequest();

        //注册httpRequest.readyState改变时会回调的函数,httpRequest.
        //readyState共有5个可能的值,
        //0	UNSENT (未打开)	open()方法还未被调用;
        //1	OPENED  (未发送)	send()方法还未被调用;
        //2	HEADERS_RECEIVED (已获取响应头)	send()方法已经被调用, 响应头和响应状态已经返回;
        //3	LOADING (正在下载响应体)	响应体下载中; responseText中已经获取了部分数据;
        //4	DONE (请求完成)	整个请求过程已经完毕.
        httpRequest.onreadystatechange = () => {

            if (httpRequest.readyState === 4) {
                //请求已完成
                if (httpRequest.status === 200) {
                    console.log("111data", httpRequest)
                    try {
                        const data = JSON.parse(httpRequest.response);
                        console.log("222data", data)
                        resolve({
                            isSuccess: true,
                            data: data.result || data
                        })
                    } catch (error) {
                        resolve({
                            isSuccess: true,
                            data: httpRequest.response
                        })
                    }
                }
            }

        }

        //请求的网址
        //该方法为初始化请求,第一个参数是请求的方法,比如GET,POST,PUT,第二个参数是请求的url
        httpRequest.open(method, url, true);

        //设置http请求头
        httpRequest.setRequestHeader("Content-Type", "application/json");

        //发出请求,参数为要发送的body体,如果是GET方法的话，一般无需发送body,设为空就可以
        httpRequest.send(JSON.stringify(params));
    })
}